package cn.wjdiankong.levideo.dialog;

import com.ss.android.common.applog.GlobalContext;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.UIUtil;

public class VideoPopupMenu {
	
	private PopupWindow mMenu;
	private View parentView;
	private View anchorView;
	private boolean isPlayLocalVideo = false;
	
	private View downloadView, copyView, deleteView, settingView;
	
	@SuppressLint("InflateParams")
	public VideoPopupMenu(Activity act){
		if(parentView == null){
			LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			parentView = inflater.inflate(R.layout.layout_playvideo_menu, null);
		}
		if(mMenu == null){
			mMenu = new PopupWindow(parentView,
	                WindowManager.LayoutParams.WRAP_CONTENT,
	                WindowManager.LayoutParams.WRAP_CONTENT, true);
	        mMenu.setBackgroundDrawable(null);
	        mMenu.setAnimationStyle(R.style.menushow);
	        mMenu.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 解决
	        mMenu.setTouchable(true);
	        mMenu.setOutsideTouchable(true);
		}
		
        // //必须设置该项才能保证相应按键事件
		parentView.setFocusableInTouchMode(true);
        // 点击外部消失
		parentView.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
			@Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mMenu != null && mMenu.isShowing()) {
                    mMenu.dismiss();
                }
                return true;
            }
        });
        // 避免 长安menu键盘
		parentView.setOnKeyListener(new View.OnKeyListener() {
            private long preClickTime = 0l;

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                long curClickTime = System.currentTimeMillis();
                if (keyCode == KeyEvent.KEYCODE_MENU
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (preClickTime == 0
                            || (curClickTime - preClickTime) > 200) {
                        if (mMenu.isShowing()) {
                            mMenu.dismiss();
                        }
                    }
                    preClickTime = curClickTime;
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (mMenu.isShowing()) {
                        mMenu.dismiss();
                    }
                }

                return false;
            }
        });
        mMenu.update();
        
        downloadView = parentView.findViewById(R.id.save_layout);
        downloadView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.DOWNLOAD_VIDEO);
				}
				toggleMenu();
			}
		});
        copyView = parentView.findViewById(R.id.copy_layout);
        copyView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.COPY_VIDEO);
				}
				toggleMenu();
			}
		});
        settingView = parentView.findViewById(R.id.setting_layout);
        settingView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.SETTING_VIDEO);
				}
				toggleMenu();
			}
		});
        deleteView = parentView.findViewById(R.id.del_layout);
        deleteView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.DELETE_VIDEO);
				}
				toggleMenu();
			}
		});
	}
	
	public void setMenuShowAnchorView(View view){
		anchorView = view;
	}
	
	public void setPlayLocalVideo(boolean isLocal){
		isPlayLocalVideo = isLocal;
		Logger.log("is local:"+isLocal);
		if(isPlayLocalVideo){
        	deleteView.setVisibility(View.VISIBLE);
        	copyView.setVisibility(View.GONE);
        	downloadView.setVisibility(View.GONE);
        }else{
        	deleteView.setVisibility(View.GONE);
        	copyView.setVisibility(View.VISIBLE);
        	downloadView.setVisibility(View.VISIBLE);
        }
		settingView.setVisibility(View.VISIBLE);
	}
	
	@SuppressLint("RtlHardcoded")
	public void toggleMenu(){
        if (mMenu.isShowing()) {
            mMenu.dismiss();
        } else {
            mMenu.showAtLocation(anchorView, Gravity.TOP | Gravity.RIGHT,
                    UIUtil.dip2px(GlobalContext.getContext(), 20),
                    UIUtil.dip2px(GlobalContext.getContext(), 60));
        }
    }
	
	public interface MenuClickItemListener{
		public static final int DOWNLOAD_VIDEO = 1;
		public static final int DELETE_VIDEO = 2;
		public static final int COPY_VIDEO = 3;
		public static final int SETTING_VIDEO = 4;
		void clickItem(int type);
	}
	
	private MenuClickItemListener mListener;
	public void setMenuClickItemListener(MenuClickItemListener listener){
		mListener = listener;
	}

}
